package com.bk.cyxf.lx;

import com.bk.cyxf.log.LogManager;

import jt.classic.system.ISystem;
import jt.classic.system.object.IObject;
import jt.classic.system.object.ObjectManager;
import jt.classic.system.purview.PurviewManager;
import jt.classic.system.role.IRole;
import jt.classic.system.role.RoleManager;
import jt.classic.system.user.IUser;

import org.limp.basework.MachiningException;
import org.limp.basework.SimpleBean;
import org.limp.basework.SimpleBean4Table;
import org.limp.basework.SimpleBeanMachining;
import org.limp.basework.Table;
import org.limp.basework.impl.SimpleBean4TableImpl;
import org.limp.basework.tools.BaseworkUtil;

import org.limp.mine.StringTool;

import java.sql.Connection;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;


/**
 * 来信对象处理插件类
 *
 * @author Mido
 *
 */
public class LxObjectHandler implements SimpleBeanMachining {
    public SimpleBean machining(SimpleBean bean, Table table,
        HttpServletRequest request, Connection conn, int type)
        throws MachiningException {
        // TODO Auto-generated method stub

        /*来信登记操作*/
        if (type == 0) {
            IObject obj = (IObject) bean.getResource().get("OBJECT");

            Table tbl = obj.iobject().itable("APP_LX");

            if (tbl == null) {
                throw new MachiningException(
                    "LxObjectHandler exception for save object, table 'APP_LX' is null.");
            }

            Map info = (Map) ((List) obj.icontent(tbl)).get(0);

            /*来信类型*/
            String LXTYPE = StringTool.checkString(info.get("LXTYPE"));

            String lxid = StringTool.checkString(info.get("LXID"));

            RoleManager rm = new RoleManager(conn);

            IRole role = null;

            if (LXTYPE.equals("0")) {
                /*街乡来信由街乡信访办人员统一办理*/
                role = rm.getRoleByKey("jxxfbuser");
            } else if (LXTYPE.equals("1")) {
                /*区来信由街乡信访办人员统一办理*/
                role = rm.getRoleByKey("quxfbuser");
            }

            /*信访系统管理员*/
            IRole xfManager = rm.getRoleByKey("xfmanager");

            IUser user = ISystem.getSessionUser(request);

            PurviewManager pm = new PurviewManager(conn);

            try {
                /*对信访办角色进行授权,可以对该来信进行办理*/
                pm.insert(obj.id(), obj.iname(), obj.ikey(), "ISYS_OBJECT", 3,
                    role.id(), role.iname(), "role", user, false);

                /*对信访系统管理员角色进行授权,可以对来信进行编辑\删除\查阅*/
                pm.insert(obj.id(), obj.iname(), obj.ikey(), "ISYS_OBJECT", 3,
                    xfManager.id(), xfManager.iname(), "role", user, false);
            } catch (Exception ex) {
                throw new MachiningException("purview for object error.", ex);
            }

            try {
                LogManager lm = new LogManager(conn);
                lm.log(user, lxid, "0", "lxdj", "");
            } catch (Exception ex) {
                throw new MachiningException("log lxdj error.", ex);
            }

            BaseworkUtil util = new BaseworkUtil();

            ObjectManager om = new ObjectManager(conn);

            try {
                Map xfblInfo = (Map) ((List) obj.icontent(obj.iobject().itable("APP_XFHANDLER"))).get(0);

                SimpleBean4Table sbt = new SimpleBean4TableImpl("APP_XFHANDLER");
                sbt.reload(xfblInfo);

                util.manualDelete1(sbt, conn);

                om.removeSystemRelationObject(obj.id(),
                    obj.iobject().itable("APP_XFHANDLER"));
            } catch (Exception ex) {
                throw new MachiningException("remove lxbl-empty-info error.", ex);
            }

            try {
                Map xffcInfo = (Map) ((List) obj.icontent(obj.iobject().itable("APP_XFFC"))).get(0);

                SimpleBean4Table sbt = new SimpleBean4TableImpl("APP_XFFC");
                sbt.reload(xffcInfo);

                util.manualDelete1(sbt, conn);

                om.removeSystemRelationObject(obj.id(),
                    obj.iobject().itable("APP_XFFC"));
            } catch (Exception ex) {
                throw new MachiningException("remove lxfc-empty-info error.", ex);
            }

            try {
                Map xffhInfo = (Map) ((List) obj.icontent(obj.iobject().itable("APP_XFFH"))).get(0);

                SimpleBean4Table sbt = new SimpleBean4TableImpl("APP_XFFH");
                sbt.reload(xffhInfo);

                util.manualDelete1(sbt, conn);

                om.removeSystemRelationObject(obj.id(),
                    obj.iobject().itable("APP_XFFH"));
            } catch (Exception ex) {
                throw new MachiningException("remove lxfh-empty-info error.", ex);
            }
        }

        return null;
    }
}
